package util.android.sqlite;

import java.util.HashMap;
import java.util.Map;

import util.android.AndroidUtil;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MiSQLiteOpenHelper 
extends SQLiteOpenHelper
{

	private final String NODO = "sqlstatement";
	
	private final Context context;
	
	@SuppressLint("UseSparseArrays")
	protected final Map<Integer,int[]> scriptsPorVersion = new HashMap<Integer,int[]>();
	
	private final int version;
	
	public MiSQLiteOpenHelper(Context context, String name,	int version) {
		super(context, name, null, version);
		this.context = context;
		this.version = version;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		try {
			SQLiteUtil.ejecutarScripts(context,db,0,version,scriptsPorVersion,NODO);			
			AndroidUtil.mostrarToast(context, "Base de datos creada");
		} catch (Exception e) {
			AndroidUtil.mostrarExcepcionEnToast(context, new Exception("Error al crear la base de datos",e));
		}		
	}
		

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String deVersion = "de version "+oldVersion+" a "+newVersion;
		//System.out.println(deVersion);
		
		try {
			SQLiteUtil.ejecutarScripts(context,db,oldVersion,newVersion,scriptsPorVersion,NODO);				
			AndroidUtil.mostrarToast(context, "Base de datos actualizada "+deVersion);
		} catch (Exception e) {
			AndroidUtil.mostrarExcepcionEnToast(context, new Exception("Error al actualizar la base de datos "+deVersion,e));
		}		
	}
	
}
